package paper.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import keter.util.IOUtil;

import org.apache.commons.io.FileUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import paper.dao.AccessoryDao;
import paper.dao.PaperArchiveDao;
import paper.dao.PaperDao;
import paper.dao.UserDao;
import paper.domain.Accessory;
import paper.domain.AuditStatus;
import paper.domain.Paper;
import paper.domain.User;
import paper.service.PaperService;
import paper.util.ConfigConstant;

@Service
public class PaperServiceImpl implements PaperService{
    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(Service.class);

	@Autowired
    private PaperDao dao;
	
	@Autowired
	private PaperArchiveDao padao;
	
	@Autowired
	private AccessoryDao adao;
	
    @Autowired
    private UserDao sdao;
	
	@Override
    public PaperDao getDao() {
		return dao;
	}
    
	@Override
    public AccessoryDao getAccessoryDao() {
    	return adao;
    }
 

    @Override
    public Accessory findAccessoryById(long id){
    	return adao.findOne(id);
    }
    
    @Override
    public Accessory writeFile(User account, String fileName, InputStream inputStream) throws IOException{
        String fileStoreRoot = ConfigConstant.getFileStoreRootPath();
        //确保目录存在
        IOUtil.ensureExists(fileStoreRoot + getRelStoreDir(account));
       
        //文件写入地址由3部分组成：存储根目录 + 存储相对目录 + 伪文件名(使用统一的文件名保证重复上传时只存在一个文件)
    	String fileUri = fileStoreRoot + getRelStoreDir(account) + ConfigConstant.REPORT_FILE_NAME;
    	
        logger.info("写入文件:{}", fileUri);
        FileUtils.copyInputStreamToFile(inputStream, new File(fileUri));        
        
        //数据库写入附件记录
        Accessory acc = new Accessory();
        acc.setName(fileName);//将文件真名存入数据库中，供下载后还原
        acc.setFilePath(getRelStoreDir(account));
        return adao.saveEntity(acc);
    }
    
    /**
     * <p>Method ：getStoreDir
     * <p>Description : 开题报告存储相对目录
     * @param stu
     * @return 
     * @author  顾力行-gulixing@msn.com
     */
    private String getRelStoreDir(User stu){
        String fileDir = ConfigConstant.REPORT_SAVE_PATH //开题报告子目录
               .concat(new DateTime().getYear()+"/")//当前年
               .concat(stu.getDegree()+"/")//学位
               .concat(stu.getAccount().getUnit().getCode()+"/")//组织机构代码
               .concat(stu.getAccount().getName()+"/");//账户名称
        return fileDir;
    }
    
    @Override
    public void submit(Paper paper){
    	paper.setSubmitted(new Date());
    	paper.setStatus(AuditStatus.SUBMIT.toString());
        dao.save(paper);
    }

	@Override
	public void confirm(Paper paper) {
		dao.save(paper);
	}
	
	@Override
	public void archive(Paper paper){
	  paper.setStatus(AuditStatus.ARCHIVE.toString());
	  padao.archive(paper);
	}
}
